package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Phone;
import com.ruoyi.system.service.IPhoneService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 手机Controller
 * 
 * @author ruoyi
 * @date 2025-06-10
 */
@RestController
@RequestMapping("/system/phone")
public class PhoneController extends BaseController
{
    @Autowired
    private IPhoneService phoneService;

    /**
     * 查询手机列表
     */
    @PreAuthorize("@ss.hasPermi('system:phone:list')")
    @GetMapping("/list")
    public TableDataInfo list(Phone phone)
    {
        startPage();
        List<Phone> list = phoneService.selectPhoneList(phone);
        return getDataTable(list);
    }

    /**
     * 导出手机列表
     */
    @PreAuthorize("@ss.hasPermi('system:phone:export')")
    @Log(title = "手机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Phone phone)
    {
        List<Phone> list = phoneService.selectPhoneList(phone);
        ExcelUtil<Phone> util = new ExcelUtil<Phone>(Phone.class);
        util.exportExcel(response, list, "手机数据");
    }

    /**
     * 获取手机详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:phone:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(phoneService.selectPhoneById(id));
    }

    /**
     * 新增手机
     */
    @PreAuthorize("@ss.hasPermi('system:phone:add')")
    @Log(title = "手机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Phone phone)
    {
        return toAjax(phoneService.insertPhone(phone));
    }

    /**
     * 修改手机
     */
    @PreAuthorize("@ss.hasPermi('system:phone:edit')")
    @Log(title = "手机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Phone phone)
    {
        return toAjax(phoneService.updatePhone(phone));
    }

    /**
     * 修改手机状态
     */
    @PreAuthorize("@ss.hasPermi('system:phone:edit')")
    @Log(title = "手机", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updatePhoneStatus(@RequestBody Phone phone) {
        if (phone.getStatus() == null) {
            return error("状态值不能为空");
        }
        return toAjax(phoneService.updatePhoneStatus(phone));
    }

    /**
     * 关机手机
     */
    @PreAuthorize("@ss.hasPermi('system:phone:edit')")
    @Log(title = "手机", businessType = BusinessType.UPDATE)
    @PostMapping("/shutdown/{id}")
    public AjaxResult shutdown(@PathVariable("id") Long id) {
        // 调用服务层方法关机手机
        return toAjax(phoneService.shutdownPhone(id));
    }

    /**
     * 删除手机
     */
    @PreAuthorize("@ss.hasPermi('system:phone:remove')")
    @Log(title = "手机", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(phoneService.deletePhoneByIds(ids));
    }
}
